<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="companyFormRules" :model="companyForm" label-position="left" label-width="140px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="companyForm.title" />
      </el-form-item>
      <el-form-item label="全称" prop="fullname">
        <el-input v-model="companyForm.fullname" />
      </el-form-item>
      <el-form-item label="业务范围" prop="businessScope">
        <el-input v-model="companyForm.businessScope" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="业务范围" />
      </el-form-item>
      <el-form-item label="网址" prop="siteUrl">
        <el-input v-model="companyForm.siteUrl" />
      </el-form-item>
      <el-form-item label="封面图片">
        <Upload v-model="companyForm.avatar" />
      </el-form-item>
      <el-form-item label="产业" prop="industrialID">
          <el-select v-model="companyForm.sectorsIndustrialID" placeholder="请选择">
            <el-option
              v-for="item in industrialOptions"
              :key="item.ID"
              :label="item.title"
              :value="item.ID"
            />
          </el-select>
          <el-select v-if="industriesIndustrialOptions.length > 0" v-model="companyForm.industriesIndustrialID" placeholder="请选择">
            <el-option
              v-for="item in industriesIndustrialOptions"
              :key="item.ID"
              :label="item.title"
              :value="item.ID"
            />
          </el-select>
          <el-select v-if="superSectorsIndustrialOptions.length > 0" v-model="companyForm.superSectorsIndustrialID" placeholder="请选择">
            <el-option
              v-for="item in superSectorsIndustrialOptions"
              :key="item.ID"
              :label="item.title"
              :value="item.ID"
            />
          </el-select>
          <el-select v-if="basicSectorIndustrialOptions.length > 0" v-model="companyForm.basicSectorIndustrialID" placeholder="请选择">
            <el-option
              v-for="item in basicSectorIndustrialOptions"
              :key="item.ID"
              :label="item.title"
              :value="item.ID"
            />
          </el-select>
          <el-select v-if="subsectorsSectorIndustrialOptions.length > 0" v-model="companyForm.subsectorsSectorIndustrialID" placeholder="请选择">
            <el-option
              v-for="item in subsectorsSectorIndustrialOptions"
              :key="item.ID"
              :label="item.title"
              :value="item.ID"
            />
          </el-select>
      </el-form-item>
      <el-form-item label="区域" prop="districtID">
          <el-select v-model="companyForm.stateDistrictID" placeholder="请选择">
            <el-option
              v-for="item in stateDistrictOptions"
              :key="item.ID"
              :label="item.title"
              :value="item.ID"
            />
          </el-select>
          <el-select v-if="regionDistrictOptions.length > 0" v-model="companyForm.regionDistrictID" placeholder="请选择">
            <el-option
              v-for="item in regionDistrictOptions"
              :key="item.ID"
              :label="item.title"
              :value="item.ID"
            />
          </el-select>
          <el-select v-if="countryDistrictOptions.length > 0" v-model="companyForm.countryDistrictID" placeholder="请选择">
            <el-option
              v-for="item in countryDistrictOptions"
              :key="item.ID"
              :label="item.title"
              :value="item.ID"
            />
          </el-select>
          <el-select v-if="provinceDistrictOptions.length > 0" v-model="companyForm.provinceDistrictID" placeholder="请选择">
            <el-option
              v-for="item in provinceDistrictOptions"
              :key="item.ID"
              :label="item.title"
              :value="item.ID"
            />
          </el-select>
          <el-select v-if="cityDistrictOptions.length > 0" v-model="companyForm.cityDistrictID" placeholder="请选择">
            <el-option
              v-for="item in cityDistrictOptions"
              :key="item.ID"
              :label="item.title"
              :value="item.ID"
            />
          </el-select>
          <el-select v-if="countyDistrictOptions.length > 0" v-model="companyForm.countyDistrictID" placeholder="请选择">
            <el-option
              v-for="item in countyDistrictOptions"
              :key="item.ID"
              :label="item.title"
              :value="item.ID"
            />
          </el-select>
          <el-select v-if="townDistrictOptions.length > 0" v-model="companyForm.townDistrictID" placeholder="请选择">
            <el-option
              v-for="item in townDistrictOptions"
              :key="item.ID"
              :label="item.title"
              :value="item.ID"
            />
          </el-select>
      </el-form-item>
      <el-form-item label="联系电话" prop="telephone">
        <el-input type="textarea" v-model="companyForm.telephone" />
      </el-form-item>
      <el-form-item label="注册地址" prop="address">
        <el-input v-model="companyForm.address" />
      </el-form-item>
      <el-form-item label="描述" prop="remark">
        <el-input v-model="companyForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="备注" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="createData()">
        保存并发布
      </el-button>
      <el-button v-if="companyForm.status.value <= 5" type="primary" @click="draftData()">
        保存
      </el-button>
      <el-button v-if="companyForm.status.value === 5 && companyForm.ID > 0" type="danger" @click="handleDelete(companyForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import { saveCompany, removeCompanyByID, getCompanyDetail } from '@/api/knowledge/company'
import { getIndustrialUsable } from '@/api/knowledge/industrial'
import { getDistrictUseableList } from '@/api/geography/district'
import Upload from '@/components/Upload/SingleImage2' // secondary package based on el-pagination
import waves from '@/directive/waves' // waves directive
import { } from '@/utils'

export default {
  name: 'ComplexTable',
  components: { Upload},
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      companyForm: {
        code: '',
        title: '',
        companyID:0,
        employeeID:0,
        remark: '',
        status: {
          label: '',
          value: 1
        }
      },
      industrialOptions:[],                      //部门
      industriesIndustrialOptions:[],            //产业
      superSectorsIndustrialOptions:[],          //行业
      basicSectorIndustrialOptions:[],           //基本行业
      subsectorsSectorIndustrialOptions:[],      //子行业

      stateDistrictOptions:[],          //州
      regionDistrictOptions:[],         //区
      countryDistrictOptions:[],        //国家
      provinceDistrictOptions:[],       //省
      cityDistrictOptions:[],          ///市
      countyDistrictOptions:[],         //县
      townDistrictOptions:[],           //县
      companyFormRules: []
    }
  },
  watch:{
      "companyForm.stateDistrictID":function(districtID){
        districtID = parseInt(districtID)
        if(districtID){
          this.regionDistrictOptions = []
          this.countryDistrictOptions = []
          this.provinceDistrictOptions = []
          this.cityDistrictOptions = []
          this.countyDistrictOptions = []
          this.townDistrictOptions = []
          this.townDistrictOptions = []
            getDistrictUseableList({
              districtID:districtID
            }).then(response => {
              if (response.status === 200) {
                this.regionDistrictOptions = response.data.list
              }
            })
        }
      },
      "companyForm.regionDistrictID":function(districtID){
        districtID = parseInt(districtID)
        if(districtID){
          this.countryDistrictOptions = []
          this.provinceDistrictOptions = []
          this.cityDistrictOptions = []
          this.countyDistrictOptions = []
          this.townDistrictOptions = []
          this.townDistrictOptions = []
            getDistrictUseableList({
              districtID:districtID
            }).then(response => {
              if (response.status === 200) {
                this.countryDistrictOptions = response.data.list
              }
            })
        }
      },
      "companyForm.countryDistrictID":function(districtID){
        districtID = parseInt(districtID)
        if(districtID){
          this.provinceDistrictOptions = []
          this.cityDistrictOptions = []
          this.countyDistrictOptions = []
          this.townDistrictOptions = []
          this.townDistrictOptions = []
            getDistrictUseableList({
              districtID:districtID
            }).then(response => {
              if (response.status === 200) {
                this.provinceDistrictOptions = response.data.list
              }
            })
        }
      },
      "companyForm.provinceDistrictID":function(districtID){
        districtID = parseInt(districtID)
        if(districtID){
          this.countyDistrictOptions = []
          this.townDistrictOptions = []
          this.townDistrictOptions = []
            getDistrictUseableList({
              districtID:districtID
            }).then(response => {
              if (response.status === 200) {
                this.cityDistrictOptions = response.data.list
              }
            })
        }
      },
      "companyForm.cityDistrictID":function(districtID){
        districtID = parseInt(districtID)
        if(districtID){
          this.countyDistrictOptions = []
          this.townDistrictOptions = []
          this.townDistrictOptions = []
            getDistrictUseableList({
              districtID:districtID
            }).then(response => {
              if (response.status === 200) {
                this.countyDistrictOptions = response.data.list
              }
            })
        }
      },
      "companyForm.countyDistrictID":function(districtID){
        this.townDistrictOptions = []
        districtID = parseInt(districtID)
        if(districtID){
          getDistrictUseableList({
            districtID:districtID
          }).then(response => {
            if (response.status === 200) {
              this.townDistrictOptions = response.data.list
            }
          })
        }
      },
      "companyForm.sectorsIndustrialID":function(industrialID){  
          industrialID = parseInt(industrialID)
          if(industrialID){
              this.industriesIndustrialOptions = [];            //产业
              this.superSectorsIndustrialOptions = [];          //行业
              this.basicSectorIndustrialOptions = [];           //基本行业
              this.subsectorsSectorIndustrialOptions = [];      //子行业
              getIndustrialUsable({            
                industrialID:industrialID
              }).then(response => {
                if (response.status === 200) {
                  this.industriesIndustrialOptions = response.data.list
                }
              })
          }   

      },
      "companyForm.industriesIndustrialID":function(){   
          industrialID = parseInt(industrialID)
          if(industrialID){
              this.superSectorsIndustrialOptions = [];          //行业
              this.basicSectorIndustrialOptions = [];           //基本行业
              this.subsectorsSectorIndustrialOptions = [];      //子行业
              getIndustrialUsable({            
                industrialID:industrialID
              }).then(response => {
                if (response.status === 200) {
                  this.superSectorsIndustrialOptions = response.data.list
                }
              })
          }

      },
      "companyForm.superSectorsIndustrialID":function(){   
          industrialID = parseInt(industrialID)
          if(industrialID){
            this.basicSectorIndustrialOptions = [];           //基本行业
            this.subsectorsSectorIndustrialOptions = [];      //子行业
            getIndustrialUsable({            
              industrialID:industrialID
            }).then(response => {
              if (response.status === 200) {
                this.basicSectorIndustrialOptions = response.data.list
              }
            })
          }

      },
      "companyForm.basicSectorIndustrialID":function(industrialID){   
          industrialID = parseInt(industrialID)
          if(industrialID){
            this.subsectorsSectorIndustrialOptions = [];      //子行业
            getIndustrialUsable({            
              industrialID:industrialID
            }).then(response => {
              if (response.status === 200) {
                this.subsectorsSectorIndustrialOptions = response.data.list
              }
            })
          }

      }
  },
  created(options) {
    this.loadEmployeeList()
    this.loadDistrictList()
    let parentCompanyID = this.$route.query && this.$route.query.parentCompanyID
    if(parentCompanyID != undefined){
      this.companyForm.companyID = parentCompanyID
    }
    let companyID = this.$route.query && this.$route.query.companyID
    if (companyID !== undefined) {
      companyID = parseInt(companyID)
      if (!isNaN(companyID)) {
        this.listLoading = true
        const formData = {
          'mechanismID': companyID
        }
        getCompanyDetail(formData).then(response => {
          if (response.status === 200) {
            this.companyForm = response.data
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
    console.log(companyID)
  },
  methods: {
    loadEmployeeList() {
      getIndustrialUsable().then(response => {
        if (response.status === 200) {
          this.industrialOptions = response.data.list
        }
      })
    },
    loadDistrictList() {

      getDistrictUseableList().then(response => {
        if (response.status === 200) {
          this.stateDistrictOptions = response.data.list
        }
      })
    },
    handleCannel() {
      this.$router.push({ path: 'company' })
    },
    draftData() {
      this.listLoading = true
      const _companyForm = this.companyForm
      _companyForm.status = 1
      if (_companyForm.ID > 0) {
        _companyForm.mechanismID = _companyForm.ID
      }
      saveCompany(_companyForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'company' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    createData() {
      this.listLoading = true
      const _companyForm = this.companyForm
      _companyForm.status = 6
      if (_companyForm.ID > 0) {
        _companyForm.mechanismID = _companyForm.ID
      }
      saveCompany(_companyForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'company' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row, index) {
      const fromdata = {
        'companyID': row.ID
      }
      removeCompanyByID(fromdata).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'company' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
